package com.enterprise.controller.manage;

import com.enterprise.cache.FrontCache;
import com.enterprise.controller.BaseController;
import com.enterprise.entity.Image;
import com.enterprise.entity.Lawyer;
import com.enterprise.service.ImageService;
import com.enterprise.service.LawyerService;
import com.enterprise.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO 律师列表
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-08-09 20:40
 **/
@Controller
@RequestMapping("/manage/lawyer")
public class LawyerAction  extends BaseController<Lawyer> {

    private static final String page_toList = "/manage/lawyer/lawyerList";
    private static final String page_toEdit = "/manage/lawyer/lawyerEdit";
    private static final String page_toAdd = "/manage/lawyer/lawyerEdit";

    @Autowired
    private LawyerService lawyerService;

    @Autowired
    private ImageService imageService;
    /**
     * 缓存类
     */
    @Autowired
    private FrontCache frontCache;

    @Override
    public Services<Lawyer> getService() {
        return lawyerService;
    }

    private LawyerAction(){
        super.page_toList = page_toList;
        super.page_toEdit = page_toEdit;
        super.page_toAdd = page_toAdd;
    }

    /**
     * 1.新增律师信息
     * @param request
     * @param lawyer
     * @param flushAttrs
     * @return
     */
    @Override
    public String insert(HttpServletRequest request, @ModelAttribute("e")Lawyer lawyer, RedirectAttributes flushAttrs) throws Exception {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        lawyer.setCreateBy("admin");
        lawyer.setCreateTime(date.format(new Date()));
        lawyer.setUpdateTime(date.format(new Date()));
        lawyer.setIsDelete(0);
        getService().insert(lawyer);

        Image image = new Image();
        image.setImage(lawyer.getImage());
        image.setTitle(lawyer.getLawyerName());
        imageService.insert(image);
        insertAfter(lawyer);
        frontCache.loadLawyer();
        addMessage(flushAttrs,"操作成功！");
        return "redirect:selectList";
    }

    /**
     * 2.更新
      * @param request
     * @param lawyer
     * @param flushAttrs
     * @return
     * @throws Exception
     */
    @Override
    public String update(HttpServletRequest request, @ModelAttribute("e")Lawyer lawyer,RedirectAttributes flushAttrs) throws Exception {
        getService().update(lawyer);
        insertAfter(lawyer);
        frontCache.loadLawyer();
        addMessage(flushAttrs,"操作成功！");
        return "redirect:selectList";
    }

    /**
     * 3.删除
     * @param request
     * @param lawyer
     * @param flushAttrs
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    public String delete(HttpServletRequest request, @ModelAttribute("e")Lawyer lawyer,RedirectAttributes flushAttrs) throws Exception {
        getService().delete(lawyer);
        insertAfter(lawyer);
        addMessage(flushAttrs,"操作成功！");
        frontCache.loadLawyer();
        return "redirect:selectList";
    }


    /**
     * 4.删除所有
     * @param request
     * @param ids
     * @param lawyer
     * @param flushAttrs
     * @return
     */
    @Override
    public String deletes(HttpServletRequest request,String[] ids, @ModelAttribute("e")Lawyer lawyer, RedirectAttributes flushAttrs) throws Exception {
        getService().deletes(ids);
        frontCache.loadLawyer();
        addMessage(flushAttrs,"操作成功！");
        return "redirect:selectList";
    }


    /**
     * 5.查询律师详情信息
     * @param request
     * @param lawyerId
     * @param flushAttrs
     * @return
     */
    @RequestMapping("/lawyerInfo")
    public String lawyerInfo(HttpServletRequest request,String lawyerId,RedirectAttributes flushAttrs){
        request.setAttribute("lawyer", getService().selectById(lawyerId));
        addMessage(flushAttrs,"操作成功！");
        return "/modern/lawyer/lawyerInfo";
    }
}
